package entities;

import java.io.Serializable;
import javax.persistence.*;

@Embeddable
public class DeclaresPK implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@ManyToOne
	@JoinColumn(name="user", referencedColumnName="userId", nullable=false)
	private RegisteredUser user;
	
	@ManyToOne
	@JoinColumn(name="ability", referencedColumnName="name", nullable=false)
	private Ability ability;
	
	public DeclaresPK(RegisteredUser user, Ability ability) {
		super();
		this.user = user;
		this.ability = ability;
	}

	public DeclaresPK() {
		// TODO Auto-generated constructor stub
	}

	public RegisteredUser getUser() {
		return user;
	}

	public Ability getAbility() {
		return ability;
	}
	
	@Override
	public boolean equals(Object o){
		try{
			if(o == null){
				return false;
			}
			DeclaresPK key = (DeclaresPK)o;
			return user.equals(key.user) && ability.equals(key.ability);
		} catch (ClassCastException e){
			return false;
		}
	}
	
	@Override
	public int hashCode(){
		return ("" + user.getUserId() + " " + ability.getName()).hashCode();
	}

}
